const net = require('net');
const fs = require('fs');
const CryptoJS = require("crypto-js");
const http = require('http');
const crypto = require('crypto');
const password = crypto.pseudoRandomBytes(96).toString('base64');
const NatAPI = require('nat-api')
const client = new NatAPI()

client.map(4000, function (err) {
    if (err) return console.log('Error', err)
    console.log((new Date()), "Port 4000 open.");
  })
console.log((new Date()), "Resolving node IP...");
http.get({
    "path": "/",
    "port": 80,
    "hostname": "icanhazip.com",
    "agent": false,
    "method": "GET",
}, data => {
    data.on("data", data => {
        console.log((new Date()), "Domain name", CryptoJS.AES.encrypt(data.toString(), password).toString());
    });
})

console.log((new Date()), "Password", password);

net.createServer(server => {

    server.on("connect", () => {});

    server.on("data", (data) => {
        data = data.toString('utf8');
        data = CryptoJS.AES.decrypt(data, password);
        data = data.toString(CryptoJS.enc.Utf8);
        data = data.replace("\r\n", "");

        if (data.split(" ")[0] == "GET")
        {
            const text = fs.readFileSync(data.split(' ')[1]).toString('utf8');
            const content = CryptoJS.AES.encrypt(text, password).toString();
            server.write(content);

            console.log((new Date()), "Sent", content);
        }
    })

}).listen(4000, '0.0.0.0');